package at.irian.jsfatwork.broker.web.model;

import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import at.irian.jsfatwork.broker.domain.client.ClientRepository;
import at.irian.jsfatwork.broker.domain.client.Client;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.Map;

/**
 * @author Thomas Spiegl
 */
@Component
@Scope("singleton")
public class AuthorizationBean {

    @Autowired
    private ClientRepository clientRepository;


    /**
     * @return true if the user is currently logged in (i.e. authenticated)
     */
    public boolean isLoggedIn() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
        return request.getUserPrincipal() != null;
    }

    /**
     * @return true if the login just failed
     */
    public boolean isLoginFailure() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Map<String,String> requestParams = facesContext.getExternalContext().getRequestParameterMap();
        String loginFailed = requestParams.get("loginfailed");
        return loginFailed != null && loginFailed.equals("1");
    }

    /**
     * @return true if the user just logged out
     */
    public boolean isAfterLogout() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Map<String,String> requestParams = facesContext.getExternalContext().getRequestParameterMap();
        String loginFailed = requestParams.get("afterlogout");
        return loginFailed != null && loginFailed.equals("1");
    }

    /**
     * @return true if a normal (first time) login is happening
     */
    public boolean isNormalLogin() {
        return !isLoginFailure() && !isAfterLogout();
    }

    
    public Client getAuthenticatedClient() {
        String userName = getUserName();
        return userName != null ? clientRepository.getClientByUserName(userName) : null;
    }

    public boolean isUserClientOnly() {
        return isUserInRole("ROLE_USER") && !isUserInRole("ROLE_ADMIN");
    }

    public boolean isUserClient() {
        return isUserInRole("ROLE_USER");
    }

    public boolean isUserAdmin() {
        return isUserInRole("ROLE_ADMIN");
    }

    private boolean isUserInRole(String role) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
        return request.isUserInRole(role);
    }

    public String getUserName() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
        Principal p = request.getUserPrincipal();
        if (p != null) {
            return p.getName();
        } else {
            return null;
        }

    }
    

}
